package cn.example.vip.mall.goods.controller;

import cn.example.vip.mall.util.RespResult;
import cn.example.vip.mall.goods.entity.CategoryBrand;
import cn.example.vip.mall.goods.service.CategoryBrandService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin	// 生产环境需要去掉
@RestController
@RequestMapping("/categoryBrand")
public class CategoryBrandController {
	@Autowired
	CategoryBrandService categoryBrandService;
	
	/**
	 * 添加 categoryBrand
	 */
	@PostMapping("/add")
	public RespResult add(@RequestBody CategoryBrand categoryBrand) {
		if(categoryBrandService.save(categoryBrand)){
			return RespResult.ok();
		}else {
			return RespResult.error();
		}
	}
	
	/**
	 * 根据 brandId 更新 categoryBrand
	 */
	@PostMapping("/update/{categoryId}/{brandId}")
	public RespResult update(@RequestBody CategoryBrand categoryBrand,
							 @PathVariable("categoryId") Integer categoryId,
							 @PathVariable("brandId") Integer brandId) {
		return RespResult.ok(categoryBrandService.updateByCategoryBrand(categoryBrand, categoryId, brandId));
	}
	
	/**
	 * 根据 categoryId 删除 categoryBrand
	 */
	@DeleteMapping("/delete")
	public RespResult delete(@RequestBody CategoryBrand categoryBrand) {
		return RespResult.ok(categoryBrandService.deleteByCategory(categoryBrand));
	}
	
	/**
	 * 查找 相似的全部 brandId
	 */
	@PostMapping("/category/search")
	public RespResult<List<CategoryBrand>> searchByCategoryId(@PathVariable CategoryBrand categoryBrand) {
		List<CategoryBrand> categoryList = categoryBrandService.queryListByCategory(categoryBrand);
		return RespResult.ok(categoryList);
	}
	
	/**
	 * 分页查找 相似的全部 brandId
	 */
	@PostMapping("/category/search/{page}/{size}")
	public RespResult<List<CategoryBrand>> searchByCategoryId(@RequestBody CategoryBrand categoryBrand,
															  @PathVariable Long page,
															  @PathVariable Long size) {
		Page<CategoryBrand> categoryPageList = categoryBrandService.queryPageListByCategory(categoryBrand, page, size);
		return RespResult.ok(categoryPageList);
	}
	
	/**
	 * 查找 相似的全部 categoryId
	 */
	@PostMapping("/brand/search")
	public RespResult<List<CategoryBrand>> searchByBrandId(@RequestBody CategoryBrand categoryBrand) {
		List<CategoryBrand> categoryList = categoryBrandService.queryListByBrand(categoryBrand);
		return RespResult.ok(categoryList);
	}
	
	/**
	 * 分页查找 相似的全部 categoryId
	 */
	@PostMapping("/brand/search/{page}/{size}")
	public RespResult<List<CategoryBrand>> searchByBrandId(@RequestBody CategoryBrand categoryBrand,
														   @PathVariable Long page,
														   @PathVariable Long size) {
		Page<CategoryBrand> categoryPageList = categoryBrandService.queryPageListByBrand(categoryBrand, page, size);
		return RespResult.ok(categoryPageList);
	}
	
}
